﻿<Page x:Class="Wokhan.WindowsFirewallNotifier.Console.UI.Pages.Connections"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      DataContext="{Binding RelativeSource={RelativeSource Self}}"
      mc:Ignorable="d" 
      d:DesignHeight="400" d:DesignWidth="800"
	Title="Connections">
    <DockPanel>
        <Border Background="#FFFF7B7B" Style="{StaticResource NonAdminPanel}">
            <WrapPanel>
                <Label Content="Information provided here might be inaccurate for non-admin users." />
                <Button x:Name="btnRestartAdmin" Click="btnRestartAdmin_Click">
                    <Grid>
                        <Path Height="16" HorizontalAlignment="Left" Stretch="Uniform" Fill="#FF5353F7" Data="{StaticResource Image_Shield}" />
                        <Label Margin="20,0,0,0" Padding="0" Content="restart as admin" />
                    </Grid>
                </Button>
            </WrapPanel>
        </Border>
        <Border Style="{StaticResource ToolBarPanel}">
            <Grid>
                <WrapPanel HorizontalAlignment="Left">
                    <ToggleButton IsChecked="{Binding IsTrackingEnabled}">
                        <Grid>
                            <Path HorizontalAlignment="Left" Width="10" Stretch="Uniform" Fill="Green">
                                <Path.Style>
                                    <Style TargetType="Path">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsTrackingEnabled, FallbackValue=True}" Value="True">
                                                <Setter Property="Data" Value="{StaticResource Image_Stop}" />
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding IsTrackingEnabled}" Value="False">
                                                <Setter Property="Data" Value="{StaticResource Image_Start}" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Path.Style>
                            </Path>
                            <Label Margin="15,0,0,0" Content="Auto-refresh" />
                        </Grid>
                    </ToggleButton>
                    <TextBlock VerticalAlignment="Center" Text="{Binding connectionsView.Count, StringFormat='Active connection(s): {0}', FallbackValue='No active connection'}" />
                </WrapPanel>
                <WrapPanel HorizontalAlignment="Right">
                    <Border BorderBrush="White" Background="White" BorderThickness="0" CornerRadius="8" Padding="5,2" Margin="0,0,5,0">
                        <WrapPanel>
                            <Path Fill="#FFA2A2FF" Data="{StaticResource Image_UpdateRate}"/>
                            <Label Margin="4,0" Content="Update rate:" />
                            <ComboBox IsEditable="False" Width="70" ItemsSource="{Binding Intervals, StringFormat='\{0\}s'}" SelectedValue="{Binding Interval}" />
                        </WrapPanel>
                    </Border>
                    <ToggleButton x:Name="btnHelp">
                        <Path Fill="Chocolate" Data="{StaticResource Image_Help}"/>
                    </ToggleButton>
                </WrapPanel>
            </Grid>
        </Border>
        <Grid>
            <DataGrid BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ColumnWidth="*" GridLinesVisibility="None" SelectionMode="Single" IsReadOnly="True" ItemsSource="{Binding connectionsView}" AutoGenerateColumns="False" HeadersVisibility="Column" HorizontalAlignment="Stretch">
                <DataGrid.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <StackPanel>
                                                <Grid Background="White" DataContext="{Binding Items[0]}">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition />
                                                    </Grid.ColumnDefinitions>
                                                    <TextBlock FontWeight="Bold" Text="{Binding GroupKey}" HorizontalAlignment="Left" VerticalAlignment="Center" />
                                                    <Rectangle Grid.Column="1" VerticalAlignment="Center" Margin="15,0,0,0" HorizontalAlignment="Stretch" Height="1" Fill="Gray" />
                                                </Grid>
                                                <ItemsPresenter />
                                            </StackPanel>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <DataGridRowsPresenter/>
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                    </GroupStyle>
                </DataGrid.GroupStyle>
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
                        <Setter Property="VerticalAlignment" Value="Center" />
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsDying}" Value="True">
                                <Setter Property="Background" Value="Orange" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsNew}" Value="True">
                                <Setter Property="Background" Value="LightBlue" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.RowStyle>
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Owner" SortMemberPath="Owner">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Image Source="{Binding Icon,Mode=OneTime}" Margin="5" Width="16" Height="16" VerticalAlignment="Center" HorizontalAlignment="Left" />
                                    <TextBlock Margin="30,0,0,0" VerticalAlignment="Center" Text="{Binding Owner, Mode=OneTime}" />
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="Protocol" Binding="{Binding Protocol, Mode=OneTime}" />
                    <DataGridTextColumn Header="LocalAddress" Binding="{Binding LocalAddress, Mode=OneTime}" />
                    <DataGridTextColumn Header="LocalPort" Binding="{Binding LocalPort, Mode=OneTime}" />
                    <DataGridTextColumn Header="RemoteAddress" Binding="{Binding RemoteAddress, Mode=OneTime}" />
                    <DataGridTextColumn Header="RemotePort" Binding="{Binding RemotePort, Mode=OneTime}" />
                    <DataGridTextColumn Header="State" Binding="{Binding State, Mode=OneWay}" />
                    <DataGridTextColumn Header="CreationTime" Binding="{Binding CreationTime, Mode=OneTime}" />
                    <!--<DataGridTextColumn Header="FirewallRule" Binding="{Binding FirewallRule.Count, Mode=OneWay}" />-->
                </DataGrid.Columns>
            </DataGrid>
            <Border Style="{StaticResource InfoBlock}">
                <TextBlock>
            This screen displays all active connections on this computer, updated every second (by default).
Closed connections (in orange) will remain displayed a few seconds before disappearing.
                </TextBlock>
            </Border>
        </Grid>
    </DockPanel>
</Page>
